package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DeptConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.workbench.domain.PhysicalUnitImportTemplate;
import com.ruoyi.workbench.domain.WbCountyUnit;
import com.ruoyi.workbench.domain.WbLeafUnit;
import com.ruoyi.workbench.domain.WbPhysicalUnit;
import com.ruoyi.workbench.service.IWbLeafLeafUnitService;
import com.ruoyi.workbench.service.IWbLeafUnitService;
import com.ruoyi.workbench.service.IWbPhysicalUnitService;
import com.ruoyi.workbench.utils.Generator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 叶子单元Controller
 *
 * @author hanjiafeng
 * @date 2021-02-24
 */
@Controller
@RequestMapping("/workbench/leaf_unit_city")
public class WbLeafUnitCityController extends BaseController {
    private final String prefix = "workbench/leaf_unit_city";
    private final IWbLeafUnitService wbLeafUnitService;
    private final ISysDeptService sysDeptService;
    private final IWbLeafLeafUnitService leafLeafUnitService;
    private final IWbPhysicalUnitService wbPhysicalUnitService;

    public WbLeafUnitCityController(IWbLeafUnitService wbLeafUnitService, ISysDeptService sysDeptService, IWbPhysicalUnitService wbPhysicalUnitService, IWbLeafLeafUnitService leafLeafUnitService) {
        this.wbLeafUnitService = wbLeafUnitService;
        this.sysDeptService = sysDeptService;
        this.wbPhysicalUnitService = wbPhysicalUnitService;
        this.leafLeafUnitService = leafLeafUnitService;
    }

    @RequiresPermissions("workbench:leaf_unit_city:view")
    @GetMapping()
    public String leafUnit() {
        return prefix + "/leaf_unit_city";
    }

    /**
     * 查询叶子单元列表
     */
    @RequiresPermissions("workbench:leaf_unit_city:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WbPhysicalUnit wbPhysicalUnit, HttpServletRequest httpServletRequest) {
        startPage();
        Long unit3Id = Generator.parseLong(httpServletRequest, "unit3Id");
        Long unit4Id = Generator.parseLong(httpServletRequest, "unit4Id");
        Long unit5Id = Generator.parseLong(httpServletRequest, "unit5Id");
        WbCountyUnit wbCountyUnit = new WbCountyUnit();
        wbCountyUnit.setUnit3(Generator.initDept(unit3Id));
        wbCountyUnit.setUnit4(Generator.initDept(unit4Id));
        wbCountyUnit.setUnit5(Generator.initDept(unit5Id));
        wbCountyUnit.setUnit6(wbPhysicalUnit);

        List<WbCountyUnit> physicalUnitList = wbPhysicalUnitService.selectWbCountyUnit(wbCountyUnit);
        return getDataTable(physicalUnitList);
    }

    @PostMapping("/selectChildrenUnitByParentId")
    @ResponseBody
    public List<WbPhysicalUnit> selectChildrenUnitByParentId(@RequestParam("parentId") Long parentId, @RequestParam("currentLevel") Byte currentLevel) {
        WbPhysicalUnit physicalUnit = new WbPhysicalUnit();
        physicalUnit.setParentId(parentId);
        physicalUnit.setLevel(currentLevel);
        return wbPhysicalUnitService.selectWbPhysicalUnit(physicalUnit);
    }

    /**
     * 导出叶子单元列表
     */
    @RequiresPermissions("workbench:leaf_unit_city:export")
    @Log(title = "所辖村庄", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WbPhysicalUnit wbPhysicalUnit, HttpServletRequest httpServletRequest) {
        Long unit3Id = Generator.parseLong(httpServletRequest, "unit3Id");
        Long unit4Id = Generator.parseLong(httpServletRequest, "unit4Id");
        Long unit5Id = Generator.parseLong(httpServletRequest, "unit5Id");
        WbCountyUnit wbCountyUnit = new WbCountyUnit();
        wbCountyUnit.setUnit3(Generator.initDept(unit3Id));
        wbCountyUnit.setUnit4(Generator.initDept(unit4Id));
        wbCountyUnit.setUnit5(Generator.initDept(unit5Id));
        wbCountyUnit.setUnit6(wbPhysicalUnit);

        List<WbCountyUnit> physicalUnitList = wbPhysicalUnitService.selectWbCountyUnit(wbCountyUnit);
        ExcelUtil<WbCountyUnit> util = new ExcelUtil<>(WbCountyUnit.class);
        return util.exportExcel(physicalUnitList, "所辖社区", ShiroUtils.getLoginName());
    }

    /**
     * 新增叶子单元
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:add")
    @Log(title = "所辖村庄", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest httpServletRequest) {
        String unit3Id = httpServletRequest.getParameter("unit3Id");
        String unit4Id = httpServletRequest.getParameter("unit4Id");
        String unit5Id = httpServletRequest.getParameter("unit5Id");
        String unit6Name = httpServletRequest.getParameter("unit6Name");
        //祖先id
        String ancestors = "100," + unit3Id + "," + unit4Id + "," + unit5Id;
        //父id
        long parentId = Long.parseLong(unit5Id);
        WbPhysicalUnit wbPhysicalUnit = new WbPhysicalUnit();
        wbPhysicalUnit.setName(unit6Name);
        wbPhysicalUnit.setParentId(parentId);
        wbPhysicalUnit.setAncestors(ancestors);
        wbPhysicalUnit.setLevel(DeptConstants.UNIT_LEVEL_6);
        SysDept unit5 = sysDeptService.selectDeptById(Long.valueOf(unit5Id));
        wbPhysicalUnit.setType(unit5.getType());

        return toAjax(wbPhysicalUnitService.insertWbPhysicalUnit(wbPhysicalUnit));
    }

    /**
     * 修改叶子单元
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        WbPhysicalUnit wbPhysicalUnit = wbPhysicalUnitService.selectByPrimaryKey(id);
        modelMap.put("wbPhysicalUnit", wbPhysicalUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:edit")
    @Log(title = "所辖村庄", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbPhysicalUnit wbPhysicalUnit) {
        return toAjax(wbPhysicalUnitService.updateByPrimaryKeySelective(wbPhysicalUnit));
    }

    @Log(title = "所辖小区", businessType = BusinessType.IMPORT)
    @RequiresPermissions("workbench:leaf_unit_city:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<PhysicalUnitImportTemplate> util = new ExcelUtil<>(PhysicalUnitImportTemplate.class);
        List<PhysicalUnitImportTemplate> physicalUnitList = util.importExcel(file.getInputStream());
        String message = wbPhysicalUnitService.importWbPhysicalUnit(physicalUnitList);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("workbench:leaf_unit_city:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<PhysicalUnitImportTemplate> util = new ExcelUtil<>(PhysicalUnitImportTemplate.class);
        return util.importTemplateExcel("部门数据");
    }

    /**
     * 删除叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit_city:remove")
    @Log(title = "所辖村庄", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbLeafUnitService.deleteWbLeafUnitByIds(ids));
    }

    /**
     * 选择登录名领导最高级的部门
     *
     * @return 部门
     */
    @PostMapping("/getMyDept")
    @ResponseBody
    public SysDept getMyDept() {
        SysUser currentUser = ShiroUtils.getSysUser();
        SysDept sysDept = currentUser.getDept();
        if (Objects.isNull(sysDept.getType())) {
            ShiroUtils.logout();
        }
        return currentUser.getDept();
    }

    /**
     * 根据七级单元id选择8级单元
     *
     * @param unit7Id unit7Id
     * @return 八级
     */
    @GetMapping("/towerList")
    @ResponseBody
    public List<WbLeafUnit> towerList(@RequestParam(name = "id") Long unit7Id) {
        return leafLeafUnitService.selectChildrenUnitByParentId(unit7Id);
    }
}
